import UIKit
import Flutter

@available(iOS 9.0, *)
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, WXApiDelegate, FATAppletButtonOpenTypeDelegate {
    
    class func sharedInstance() -> UIApplicationDelegate {
        return (UIApplication.shared.delegate)!
    }
    
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    GeneratedPluginRegistrant.register(with: self)
    
      
    WXApi.registerApp("wx83cf55a5a3baaba0", universalLink: "https://com.anHuanJia.xinanli/")
          
    AMapServices.shared().apiKey = "76ffe13cd5b5e54de8a438aaec4fc8ca"
    
    UMConfigure.initWithAppkey("63bf6bded64e6861391563c8", channel: "APPStore")
    
    //视频直播
    FlutterNativeLivePlayPlugin.register(with: self.registrar(forPlugin: "FlutterNativeAppJumpPlugin")!)
    
    //密码加密
    FlutterNativePWDAESPlugin.register(with: self.registrar(forPlugin: "FlutterNativePWDAESPlugin")!)
    
    //微信小程序
    FlutterNativeWXSmallPlugin.register(with: self.registrar(forPlugin: "FlutterNativeWXSmallPlugin")!)
    
    //微信分享
    FlutterNativeWXSharePlugin.register(with: self.registrar(forPlugin: "FlutterNativeWXSharePlugin")!)
    
    //权限检测
    FlutterNativePermissionPlugin.register(with: self.registrar(forPlugin: "FlutterNativePermissionPlugin")!)
    
    //权限检测
    FlutterNativePhotoPlugin.register(with: self.registrar(forPlugin: "FlutterNativePhotoPlugin")!)
    
    // 微信 H5 支付
    WechatPayPlugin.register(with: self.registrar(forPlugin: "WechatPayPlugin")!)
    
    //alipay
    FlutterNativeAliPayPlugin.register(with: self.registrar(forPlugin: "FlutterNativeAliPayPlugin")!)
    
    //人脸核身
    FlutterNativeFacePlugin.register(with: self.registrar(forPlugin: "FlutterNativeFacePlugin")!)
    // 高德定位
    FlutterNativeLocationPlugin.register(with: self.registrar(forPlugin: "FlutterNativeLocationPlugin")!)
    // 高德定位
    AMapFlutterLocationPlugin1.register(with: self.registrar(forPlugin: "AMapFlutterLocationPlugin1")!)
    
    //打开文件
    FlutterNativeOpenFilePlugin.register(with: self.registrar(forPlugin: "FlutterNativeOpenFilePlugin")!)

    // 阿里一键登录
    ALLoginPlugin.register(with: self.registrar(forPlugin: "ALLoginPlugin")!)
      
    // 阿里一键登录
    FlutterNativeMapPOIPlugin.register(with: self.registrar(forPlugin: "FlutterNativeMapPOIPlugin")!)
      
      FATClient.shared().buttonOpenTypeDelegate = self
      
      Tools.openSmallWexx()
      FlutterMapPOIPlugin.register(with: self.registrar(forPlugin: "FlutterMapPOIPlugin")!)
    
    let flutterViewController = self.window.rootViewController!
    let nav = UINavigationController.init(rootViewController: flutterViewController)
    nav.navigationBar.isHidden = true
    self.window.rootViewController = nav

    self.window.makeKeyAndVisible()
    
        
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    ///跳转小程序
        override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
            return WXApi.handleOpenUniversalLink(userActivity, delegate: self)///写成true 也行
        }
        
        override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            let urlKey: String = url.absoluteString
            if WXApi.handleOpen(url, delegate: Tools.sharedManager() as! WXApiDelegate) {
                return true
            }
            if urlKey.contains("wx") {
                return WXApi.handleOpen(url, delegate: self)
            }else {
                //跳转支付宝钱包进行支付，处理支付结果
                AlipaySDK.defaultService().processOrder(withPaymentResult: url as URL, standbyCallback: { (resultDic: [AnyHashable : Any]?) in
                    print("openURL result: \(String(describing: resultDic))")
                        let resultStatus = resultDic!["resultStatus"] as! String
                    NotificationCenter.default.post(name: NSNotification.Name("aliPay"), object: resultStatus, userInfo:nil)
                } )
            }
            
            return true
        }
    
        
        override func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
            return WXApi.handleOpen(url, delegate: self)
        }
        
        override func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
            if WXApi.handleOpen(url, delegate: Tools.sharedManager() as! WXApiDelegate) {
                return true
            }
            return WXApi.handleOpen(url, delegate: self)
        }
        
        func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
            if WXApi.handleOpenUniversalLink(userActivity, delegate: Tools.sharedManager() as! WXApiDelegate) {
                return WXApi.handleOpenUniversalLink(userActivity, delegate: Tools.sharedManager() as! WXApiDelegate)
            }
            return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
        }
        
        override func application(_ application: UIApplication, willContinueUserActivityWithType userActivityType: String) -> Bool {
            if WXApi.handleOpenUniversalLink(userActivity!, delegate: Tools.sharedManager() as! WXApiDelegate) {
                return WXApi.handleOpenUniversalLink(userActivity!, delegate: Tools.sharedManager() as! WXApiDelegate)
            }
            return WXApi.handleOpenUniversalLink(userActivity!, delegate: self)
        }
        
    func getPhoneNumber(with appletInfo: FATAppletInfo, bindGetPhoneNumber: @escaping ([AnyHashable : Any]) -> Void) -> Bool {
//        print(appletInfo);
//        WXApi.checkUniversalLinkReady { WXULCheckStep, WXCheckULStepResult in
//        }
//        WXApi.handleOpen(T##url: URL##URL, delegate: WXApiDelegate?)
//        [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
//                NSLog(@"%@, %u, %@, %@", @(step), result.success, result.errorInfo, result.suggestion);
//            }];WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
        
//        Tools.openSmallWexx()
        return true
    }
    
}
